﻿//-----------------------------------------------------------------------
// <copyright>
// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
// </copyright>
//----------------------------------------------------------------------- 
using System;
using System.Linq;
using System.Windows.Threading;
using Fulcrum.DispatcherProxies;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Fulcrum.UnitTests.DispatcherProxies
{
    [TestClass]
    public class DispatcherProxyTargetManagerTests
    {
        public DispatcherProxyTargetManagerTests()
        {
            // nothing
        }

        [TestMethod]
        public void TestDispatcherProxyTargetManager()
        {
            var targetManager = new DispatcherProxyTargetManager();

            targetManager.AddTargetAssembly("Fulcrum.UnitTests.DispatcherProxies.dll");

            Assert.AreEqual(7, targetManager.Targets.Count);

            ValidateTarget(targetManager.Targets.ElementAt(0), "BaseProxy", "DispatcherObjectProxy", false, typeof(Base));
            ValidateTarget(targetManager.Targets.ElementAt(1), "DerivedProxy", "BaseProxy", false, typeof(Derived));
            ValidateTarget(targetManager.Targets.ElementAt(2), "DoubleDerivedProxy", "DerivedProxy", false, typeof(DoubleDerived));
            ValidateTarget(targetManager.Targets.ElementAt(3), "MyDispatcherClassProxy", "DispatcherObjectProxy", false, typeof(MyDispatcherClass));
            ValidateTarget(targetManager.Targets.ElementAt(4), "MyOtherDispatcherClassProxy", "MyDispatcherClassProxy", false, typeof(MyOtherDispatcherClass));
            ValidateTarget(targetManager.Targets.ElementAt(5), "PublicDispatcherObjectDecendantProxy", "DispatcherObjectProxy", false, typeof(PublicDispatcherObjectDecendant));
            ValidateTarget(targetManager.Targets.ElementAt(6), "DispatcherObjectProxy", null, true, typeof(DispatcherObject));
        }

        private static void ValidateTarget(DispatcherProxyTarget target, string name, string baseProxyName, bool isAbstract, Type wrappedType)
        {
            Assert.AreEqual(name, target.ProxyName);

            if (String.IsNullOrEmpty(baseProxyName))
            {
                Assert.IsNull(target.BaseProxy);
            }
            else
            {
                Assert.IsNotNull(target.BaseProxy);
                Assert.AreEqual(baseProxyName, target.BaseProxy.ProxyName);
            }

            Assert.AreEqual(isAbstract, target.IsAbstract);
            Assert.AreEqual(wrappedType.FullName, target.WrappedType);
        }
    }
}
